package com.c.characteristic;
//封装
public class TestSetGet {
    public static void main(String[] args) {
        Student student=new Student();
        student.setAge(20);
        System.out.println("您的年龄："+student.getAge());
        student.setSex("女");
        System.out.println("性别："+student.getSex());
    }
}
class Student{
    String name;
    private int age;//私有属性（本类可见）
    private String sex;
    double score;

    public int getAge() {       //进行取值
        return age;
    }

    public void setAge(int age) {   //进行赋值
        if (age>0 && age<260) {     //对输入年龄进行限制
            this.age = age;
        }else {
            //以后可以抛出异常
            this.age=18;        //默认18
        }

    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        if ("男".equals(sex) || "女".equals(sex)){        //注："男".equals(sex)必须这么写
            this.sex = sex;
        }else {
            this.sex="男";
        }

    }
}
